var app = getApp();
Page({
  data: {
    personal: false,
    individual: true,
    enterprise: true,
    yyzz_head: '../../res/upload.png',
    sy_head: '../../res/upload.png',
    cj_head: '../../res/upload.png',
    mt_head: '../../res/upload.png',
    sfz_head: '../../res/upload.png',
    sff_head: '../../res/upload.png',
    scsf_head: '../../res/upload.png',
    yhkz_head: '../../res/upload.png',
    khxkz_head: '../../res/upload.png',
    multiArray: [
      ['餐娱类', '房产汽车类'],
      ['宾馆类', '餐饮类'],
      ['住宿服务（旅馆、酒店、汽车旅馆、度假村等）', '分时使用的别墅或度假用房']
    ],
    multiIndex: [0, 0, 0],
    region: ['山西省', '太原市', '小店区'],
    regions: ['山西省', '太原市', '小店区'],
    khyk: ['中国工商银行', '中国农业银行', '中国银行'],
    brands: [],
    objectArray: [{
        brand: "中国工商银行",
        id: 0,
        array: ["中国工商银行山西省分行", "中国工商银行太原分行", "中国工商银行太原迎泽支行", "中国工商银行太原五一路支行"]
      },
      {
        brand: "中国农业银行",
        id: 1,
        array: ["中国农业银行山西省分行（不对外办理业务）", "中国农业银行太原国贸支行", "中国农业银行太原新建支行", "中国农业银行太原东汇支行"]
      },
    ],
    object: [],
    brandindex: 0,
    index1: 0,
    personals: true,
    rate:0.38,
    yezz_validity_state:0,
    header_card_validity:0,
  },
  onLoad: function () {
    var store_id = wx.getStorageSync("store_id");
    console.log(store_id)
    if(!store_id){
      this.setData({
        store_id:0
      })
      app.showToast("请登录小程序")

    }else{
      this.setData({
        store_id:wx.getStorageSync("store_id")
      })
      this.getStoreMerchant();
    }
    this.data.api_url = app.api_url;
    var objectArray = this.data.objectArray
    var brands = []
    for (var i = 0; i < objectArray.length; i++) {
      brands.push(objectArray[i].brand, )
    }
    this.setData({
      brands: brands,
      object: objectArray[this.data.brandindex].array
    })
  },

  getStoreMerchant:function(){
    var t = this;
    app.visit("account/getStoreMerchant", {
      store_id:t.data.store_id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.setData({
          info:c.data,
          merchant_type:c.data.merchant_type,
          name:c.data.name,
          mobile:c.data.mobile,
          yyzz_head:c.data.yyzz_img?t.data.api_url+c.data.yyzz_img:'../../res/upload.png',
          sy_head:c.data.syt_img?t.data.api_url+c.data.syt_img:'../../res/upload.png',
          cj_head:c.data.jycsz_img?t.data.api_url+c.data.jycsz_img:'../../res/upload.png',
          mt_head:c.data.mtz_img?t.data.api_url+c.data.mtz_img:'../../res/upload.png',
          sfz_head:c.data.fr_zheng?t.data.api_url+c.data.fr_zheng:'../../res/upload.png',
          sff_head:c.data.fr_fan?t.data.api_url+c.data.fr_fan:'../../res/upload.png',
          scsf_head:c.data.scsfz_img?t.data.api_url+c.data.scsfz_img:'../../res/upload.png',
          khxkz_head:c.data.khxkz_img?t.data.api_url+c.data.khxkz_img:'../../res/upload.png',
          yhkz_head:c.data.yhkz_img?t.data.api_url+c.data.yhkz_img:'../../res/upload.png',
          yyzz_img:c.data.yyzz_img,
          syt_img:c.data.syt_img,
          jycsz_img:c.data.jycsz_img,
          mtz_img:c.data.mtz_img,
          fr_fan:c.data.fr_fan,
          fr_zheng:c.data.fr_zheng,
          scsfz_img:c.data.scsfz_img,
          khxkz_img:c.data.khxkz_img,
          yhkz_img:c.data.yhkz_img,
          yezz_name:c.data.yezz_name,
          yezz_number:c.data.yezz_number,
          yezz_validity_s:c.data.yezz_validity_s,
          yezz_validity_e:c.data.yezz_validity_e,
          yezz_validity_state:c.data.yezz_validity_state,
          yezz_address:c.data.yezz_address,
          email:c.data.email,
          address:c.data.address,
          weixin_no:c.data.weixin_no,
          alipay_no:c.data.alipay_no,
          header_card:c.data.header_card,
          header_name:c.data.header_name,
          header_card_validity:c.data.header_card_validity,
          header_card_validity_s:c.data.header_card_validity_s,
          header_card_validity_e:c.data.header_card_validity_e,
          jsyhzh:c.data.jsyhzh,
        })
      }else{
        app.showToast(c.msg)
      }
    })
  },

  tijiao:function(e){
    var t = this;
    if(!t.data.name){
      app.showToast("请填写商户名称")
      return false;
    }
    if(!t.data.mobile){
      app.showToast("请填写注册手机号")
      return false;
    }
    if(!(/^1[3456789]\d{9}$/.test(t.data.mobile))){
      app.showToast("请填写正确手机号")
      return false;
    }
    wx.showModal({
      title: '提示',
      content: '确认提交审核吗',
      success: function (res) {
        if (res.confirm) {
          app.visit("account/tiJiaoMerchant", {
            store_id:wx.getStorageSync("store_id"),
            merchant_type:t.data.merchant_type,
            name:t.data.name,
            mobile:t.data.mobile,
            yyzz_img:t.data.yyzz_img?t.data.yyzz_img:'',
            syt_img:t.data.syt_img?t.data.syt_img:'',
            jycsz_img:t.data.jycsz_img?t.data.jycsz_img:'',
            mtz_img:t.data.mtz_img?t.data.mtz_img:'',
            fr_zheng:t.data.fr_zheng?t.data.fr_zheng:'',
            fr_fan:t.data.fr_fan?t.data.fr_fan:'',
            scsfz_img:t.data.scsfz_img?t.data.scsfz_img:'',
            yhkz_img:t.data.yhkz_img?t.data.yhkz_img:'',
            khxkz_img:t.data.khxkz_img?t.data.khxkz_img:'',
            yezz_name:t.data.yezz_name?t.data.yezz_name:'',
            yezz_number:t.data.yezz_number?t.data.yezz_number:'',
            yezz_validity_state:t.data.yezz_validity_state,
            yezz_validity_s:t.data.yezz_validity_s?t.data.yezz_validity_s:'',
            yezz_validity_e:t.data.yezz_validity_e?t.data.yezz_validity_e:'',
            yezz_address:t.data.yezz_address?t.data.yezz_address:'',
            email:t.data.email?t.data.email:'',
            address:t.data.address?t.data.address:'',
            weixin_no:t.data.weixin_no?t.data.weixin_no:'',
            alipay_no:t.data.alipay_no?t.data.alipay_no:'',
            header_card:t.data.header_card?t.data.header_card:'',
            header_name:t.data.header_name?t.data.header_name:'',
            header_card_validity_s:t.data.header_card_validity_s?t.data.header_card_validity_s:'',
            header_card_validity_e:t.data.header_card_validity_e?t.data.header_card_validity_e:'',
            header_card_validity:t.data.header_card_validity,
            jsyhzh:t.data.jsyhzh?t.data.jsyhzh:'',
          }, "", function(c) {
            console.log(c)
            if(0 == c.code){
              wx.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 5000,
                success:function () {
                  setTimeout(function () {
                    wx.navigateBack();
                  }, 3000);// 分钟转换毫秒
                }
              })
            }else{
              app.showToast(c.msg)
            }
          })
        } else {//这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })
  },

  type_radio: function (e) {
    var type_radio = e.detail.value;
    if (type_radio == '1') {
      this.setData({
        personals: true,
        personal: false,
        enterprises: false,
        enterprise: true
      })
    } else if (type_radio == '2') {
      this.setData({
        personals: false,
        personal: true,
        enterprises: false,
        enterprise: true
      })
    } else if (type_radio == '3') {
      this.setData({
        personals: false,
        personal: true,
        enterprises: true,
        enterprise: false
      })
    }
    this.setData({
      merchant_type:e.detail.value
    })
  },
  yezz_validity_state: function (e) {
    this.setData({
      yezz_validity_state:e.detail.value
    })
  },
  setalipay_no: function (e) {
    this.setData({
      alipay_no:e.detail.value
    })
  },
  setjsyhzh: function (e) {
    this.setData({
      jsyhzh:e.detail.value
    })
  },
  setheader_card: function (e) {
    this.setData({
      header_card:e.detail.value
    })
  },
  setheader_name: function (e) {
    this.setData({
      header_name:e.detail.value
    })
  },
  setName:function(e){
    console.log(e.detail.value)
    this.setData({
      name:e.detail.value
    })
  },
  setMobile:function(e){
    console.log(e.detail.value)
    this.setData({
      mobile:e.detail.value
    })
  },
  setyezz_name:function(e){
    console.log(e.detail.value)
    this.setData({
      yezz_name:e.detail.value
    })
  },
  setyezz_number:function(e){
    console.log(e.detail.value)
    this.setData({
      yezz_number:e.detail.value
    })
  },
  setyezz_address:function(e){
    console.log(e.detail.value)
    this.setData({
      yezz_address:e.detail.value
    })
  },
  setemail:function(e){
    console.log(e.detail.value)
    this.setData({
      email:e.detail.value
    })
  },
  setaddress:function(e){
    console.log(e.detail.value)
    this.setData({
      address:e.detail.value
    })
  },
  setweixin_no:function(e){
    console.log(e.detail.value)
    this.setData({
      weixin_no:e.detail.value
    })
  },
  header_card_validity:function(e){
    console.log(e.detail.value)
    this.setData({
      header_card_validity:e.detail.value
    })
  },

  yyzz_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              yyzz_head: a.data.url,
              yyzz_img: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  sy_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              sy_head: a.data.url,
              syt_img: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  cj_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              cj_head: a.data.url,
              jycsz_img: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  mt_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              mt_head: a.data.url,
              mtz_img: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['宾馆类', '餐饮类'];
            data.multiArray[2] = ['住宿服务（旅馆、酒店、汽车旅馆、度假村等）', '分时使用的别墅或度假用房'];
            break;
          case 1:
            data.multiArray[1] = ['典当类', '房地产类', '汽车销售类'];
            data.multiArray[2] = ['当铺'];
            break;
        }
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
      case 1:
        switch (data.multiIndex[0]) {
          case 0:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ['住宿服务（旅馆、酒店、汽车旅馆、度假村等）', '分时使用的别墅或度假用房'];
                break;
              case 1:
                data.multiArray[2] = ['包办伙食，宴会承包商', '就餐场所和餐馆', '饮酒场所（酒吧、酒馆、夜总会、鸡尾酒大厅、迪斯科舞厅）'];
                break;
            }
            break;
          case 1:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ['当铺'];
                break;
              case 1:
                data.multiArray[2] = ['一般承包商－住宅与商业楼', '不动产代理——房地产经纪'];
                break;
              case 2:
                data.multiArray[2] = ['活动房车销售商', '汽车货车经销商－新旧车的销售、服务、维修、零件及出租', '汽车货车经销商－专门从事旧车的销售、服务、维修、零件及出租'];
                break;
            }
            break;
        }
        data.multiIndex[2] = 0;
        break;
    }
    console.log(data.multiIndex);
    this.setData(data);
  },
  bindRegionChange: function (e) { // picker值发生改变都会触发该方法
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      box2: false,
      box1: true
    })
  },
  bindRegionChanges: function (e) { // picker值发生改变都会触发该方法
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      regions: e.detail.value,
      box4: false,
      box3: true
    })
  },
  sfz_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              sfz_head: a.data.url,
              fr_zheng: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  sff_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              sff_head: a.data.url,
              fr_fan: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  scsf_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              scsf_head: a.data.url,
              scsfz_img: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  changeDate(e) {
    this.setData({
      header_card_validity_s: e.detail.value
    });
  },
  changeDates(e) {
    this.setData({
      header_card_validity_e: e.detail.value
    });
  },

  zyzz_changeDate(e) {
    this.setData({
      yezz_validity_s: e.detail.value
    });
  },
  zyzz_changeDates(e) {
    this.setData({
      yezz_validity_e: e.detail.value
    });
  },
  yhkz_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              yhkz_head: a.data.url,
              yhkz_img: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  khxkz_image: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              khxkz_head: a.data.url,
              khxkz_img: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },

  khyk_bind: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      khyk_index: e.detail.value
    })
  },
  bindPickerChange0: function (e) {
    this.setData({
      brandindex: e.detail.value,
      index1: 0,
      box6: false,
      box5: true,
    })
    var objectArray = this.data.objectArray
    this.setData({
      object: objectArray[this.data.brandindex].array
    })
  },
  bindPickerChange1: function (e) {
    console.log(e.detail.value)
    this.setData({
      index1: e.detail.value,
      box8: false,
      box7: true,
    })
  },

})